package com.app.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;

public class MathUtil {

    public static double getRandomDouble(double min, double max) {
        Random rand = new Random();
        double yjResult = 0;
        yjResult = min + (rand.nextDouble() * (max - min));
        yjResult = (double) Math.round(yjResult * 100) / 100;
        return yjResult;
    }

    public static float getRandomFloat(float min, float max) {
        Random rand = new Random();
        float yjResult = 0;
        yjResult = min + (rand.nextFloat() * (max - min));
        yjResult = (float) Math.round(yjResult * 100) / 100;
        return yjResult;
    }
    public static double roundToTwoDecimalPlaces(String valueStr) {
        if (valueStr == null || valueStr.isEmpty()) {
            return 0.0;
        }
        try {
            BigDecimal value = new BigDecimal(valueStr);
            return value.setScale(2, RoundingMode.HALF_UP).doubleValue();
        } catch (NumberFormatException e) {
            // 可以记录日志或处理异常值
            return 0.0;
        }
    }
}
